﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.Helper;
using BCGC.BLL.BusinessObject.Resource;
using log4net;
using Jalal.Library.Utility;
using System.Collections.Generic;

public partial class Admin_Resource_Wallpaper_EditMoreSize : AdminBasePage
{
    private int Id
    {
        get
        {
            if (ViewState["Id"] == null)
                ViewState["Id"] = 0;
            return (int)ViewState["Id"];
        }
        set { ViewState["Id"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int id = Id = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["id"]) && int.TryParse(Request.QueryString["id"], out id))
            {
                Id = id;
                LoadData();
            }
            else
                Response.Redirect(CustomSiteMap.Wallpapers);

            InitControls();
        }
    }

    private void InitControls()
    {
        btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete?');");
    }

    private void LoadData()
    {
        WallpaperSize wallpaperSize = WallpaperSize.GetWallpaperSizeByWallpaperSizeId(Id);
        Wallpaper currentWallpaper = Wallpaper.GetWallpaperById(wallpaperSize.WallpaperId);
        imgWallpaperThumb.ImageUrl = currentWallpaper.ThumbnailFileUrl;
        ddlSize.SelectedValue = wallpaperSize.SizeName;
    }

    protected void Size_Changd(object sender, EventArgs e)
    {
        if (ddlSize.SelectedValue == "Other")
            plhCustomSize.Visible = true;
        else
            plhCustomSize.Visible = false;
    }

    protected void Create_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            try
            {
                Wallpaper currentWallpaper = Wallpaper.GetWallpaperById(Id);
                int width = 0;
                int height = 0;
                bool widhtHeightFound = false;
                if (ddlSize.SelectedValue == "Other")
                {
                    if (int.TryParse(tbxWidth.Text, out width) && int.TryParse(tbxHeight.Text, out height))
                        widhtHeightFound = true;
                }
                else if (ddlSize.SelectedValue != "None")
                {
                    string[] sizeParts = ddlSize.SelectedValue.Split(new char[] { 'x' });
                    if (sizeParts.Length >= 2)
                    {
                        if (int.TryParse(sizeParts[0], out width) && int.TryParse(sizeParts[1], out height))
                            widhtHeightFound = true;
                    }
                }

                bool uploadSuccess = false;
                if (widhtHeightFound)
                {
                    List<System.Drawing.Size> sizes = new List<System.Drawing.Size>();
                    sizes.Add(new System.Drawing.Size(width, height));
                    uccImageUploader.Sizes = sizes;
                    List<string> fileUrl = uccImageUploader.UploadImage();
                    if (fileUrl.Count > 0)
                    {
                        if (WallpaperSize.Update(Id, ddlSize.SelectedValue, fileUrl[0]))
                        {
                            uccResponse.SetSuccessMessage("Size wallpaper updated");
                            uploadSuccess = true;
                        }
                    }
                }

                if (!uploadSuccess)
                    uccResponse.SetFailMessage("Failed to update size wallpaper");
            }
            catch (Exception ex)
            {
                // Write the log into handled exception list
                ILog log = LogHelper.GetHandledFileLogger("EditMoreSize");
                if (log.IsErrorEnabled)
                    log.Error("Failed to update size image to wallpaper", ex);

                uccResponse.SetFailMessage("There was a problem in updating size wallpaper");
            }
        }
    }

    protected void Delete_Click(object sender, EventArgs e)
    {
        if (WallpaperSize.Delete(new List<int> { Id }))
            Response.Redirect(CustomSiteMap.Wallpapers);
        else
            uccResponse.SetFailMessage("Failed to delete size");
    }
}
